﻿using LiveChartsCore;
using LiveChartsCore.Defaults;
using LiveChartsCore.Drawing;
using LiveChartsCore.SkiaSharpView;
using LiveChartsCore.SkiaSharpView.Drawing;
using LiveChartsCore.SkiaSharpView.Drawing.Geometries;
using LiveChartsCore.SkiaSharpView.Painting;
using LiveChartsCore.SkiaSharpView.Painting.Effects;
using LiveChartsCore.SkiaSharpView.VisualElements;
using MonitorPrism_Practice.ViewModel;
using SkiaSharp;
using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace MonitorPrism_Practice.Services
{
    public class ClassLiveCharts2Line : BindableBase
    {
        public int LineIndex = 0;
        ISeries[] _lineSeries;
        public ISeries[] LineSeries
        {
            get { return _lineSeries; }
            set { SetProperty(ref _lineSeries, value); }

        }
        private Axis[] _ISeries_XAxis_Datatime;
        public Axis[] ISeries_XAxis_Datatime
        {
            get => _ISeries_XAxis_Datatime;
            set { SetProperty(ref _ISeries_XAxis_Datatime, value); }

        }
        private Axis[] _iSeries_YAxis_Normal;
        public Axis[] ISeries_YAxis_Normal
        {
            get => _iSeries_YAxis_Normal;
            set { SetProperty(ref _iSeries_YAxis_Normal, value); }
        }
        private IPaint<SkiaSharpDrawingContext> _toolTipTextPaint;
        public IPaint<SkiaSharpDrawingContext> ToolTipTextPaint
        {
            get => _toolTipTextPaint;
            set { SetProperty(ref _toolTipTextPaint, value); }
        }
        private IPaint<SkiaSharpDrawingContext> _iSeries_LegendTextPaint;
        public IPaint<SkiaSharpDrawingContext> ISeries_LegendTextPaint
        {
            get => _iSeries_LegendTextPaint;
            set { SetProperty(ref _iSeries_LegendTextPaint, value); }
        }
        private ObservableCollection<string> _lineName = new ObservableCollection<string>()
        {

        };
        public ObservableCollection<string> LineName
        {
            get => _lineName ?? new ObservableCollection<string>();
            set { SetProperty(ref _lineName, value); }
        }
        private string _lineThickness;
        public string LineThickness
        {
            get => List_LineSeries.Count > 0 ? List_LineSeries[LineIndex].Stroke.StrokeThickness.ToString() : "0";
            set
            {
                SetProperty(ref _lineThickness, value);
               
            }
        }

        /// <summary>
        /// 线性系列数组
        /// </summary>
        public ISeries[] ISeries_LineSeries { get; set; }
        /// <summary>
        /// 横坐标时间轴
        /// </summary>
        //public Axis[] ISeries_XAxis_Datatime { get; set; }
        /// <summary>
        /// 横坐标正常轴
        /// </summary>
        public Axis[] ISeries_XAxis_Normal { get; set; }
        /// <summary>
        /// 纵坐标数值轴
        /// </summary>
        public Axis[] ISeries_YAxis_Value { get; set; }
        /// <summary>
        /// 纵坐标正常轴
        /// </summary>
        //public Axis[] ISeries_YAxis_Normal { get; set; }
        /// <summary>
        /// 线性系列标签
        /// </summary>
        public LabelVisual ISeries_LabelVisual { get; set; }
        /// <summary>
        /// 图例文字
        /// </summary>
        //public IPaint<SkiaSharpDrawingContext> ISeries_LegendTextPaint { get; }
        /// <summary>
        /// 提示文字
        /// </summary>
        //public IPaint<SkiaSharpDrawingContext> ToolTipTextPaint { get; set; }

        /// <summary>
        /// 横坐标最大值
        /// </summary>
        public double ISeries_XAxis_MaxLimit = 40;
        /// <summary>
        /// 横坐标最小值
        /// </summary>
        public double ISeries_XAxis_MinLimit = 0;
        /// <summary>
        /// 纵坐标最大值
        /// </summary>
        public double ISeries_YAxis_MaxLimit = 100;
        public double ISeries_YAxis_MaxLimitRight = 500;
        /// <summary>
        /// 纵坐标最小值
        /// </summary>
        public double ISeries_YAxis_MinLimit = 0;
        /// <summary>
        /// 横坐标自动滚动值
        /// </summary>
        public double ISeries_XAxis_AutoScrollLimit = 0;
        /// <summary>
        /// 线性序列数量，最多10个系列
        /// </summary>
        public int ISeries_LineSeriesMaxCount = 3;
        /// <summary>
        /// 单一线性系列最大数据点数
        /// </summary>
        public int ISeries_SingleLineSeriesMaxPoints = 2000 - 1;
        /// <summary>
        /// 动态数据点列表已移除数量
        /// </summary>
        public int List_ObservableCollectionRemovedCount = 0;

        /// <summary>
        /// 线性序列列表
        /// </summary>
        public List<LineSeries<ObservablePoint>> List_LineSeries;
        /// <summary>
        /// 动态数据点列表
        /// </summary>
        public List<ObservableCollection<ObservablePoint>> List_ObservableCollection;
        /// <summary>
        /// 横坐标标签
        /// </summary>
        private List<string> List_ISeries_XAxis_Labels;
        /// <summary>
        /// 纵坐标标签
        /// </summary>
        private List<string> List_ISeries_YAxis_Labels;
        /// <summary>
        /// 横坐标名称
        /// </summary>
        private List<string> List_ISeries_XAxis_Name;
        /// <summary>
        /// 纵坐标名称
        /// </summary>
        private List<string> List_ISeries_YAxis_Name;
        /// <summary>
        /// 颜色列表
        /// </summary>
        private List<string> List_SolidColorPaint;
        bool isDateTimeLabelAdded = false;

        public ClassLiveCharts2Line()
        {
            List_SolidColorPaint = new List<string> { "#ff2196f3", "#fff44336", "#ff009688", "#ffffc107", "#ff00bcd4", "#ff3f51b5", "#ff8bc34a", "#ffe91e63", "#ff607d8b", "#ff464646" };

            List_LineSeries = new List<LineSeries<ObservablePoint>>();
            List_ObservableCollection = new List<ObservableCollection<ObservablePoint>>();

            for (int i = 0; i < ISeries_LineSeriesMaxCount; i++)
            {
                List_LineSeries.Add(new LineSeries<ObservablePoint>()
                {
                    DataLabelsPaint = new SolidColorPaint()
                    {
                        Color = SKColor.Parse(List_SolidColorPaint[0]),
                    }

                });
                List_ObservableCollection.Add(new ObservableCollection<ObservablePoint>());
            }

            for (int i = 0; i < List_LineSeries.Count; i++)
            {
                List_ObservableCollection[i].Add(new ObservablePoint(0, 0));//预先添加一个数据点，避免未绘制曲线（第一次）时无法设置曲线名称的问题
                List_LineSeries[i].Values = List_ObservableCollection[i];
                switch (i)
                {
                    case 0: List_LineSeries[i].Name = "温度"; break;
                    case 1: List_LineSeries[i].Name = "目标温度"; break;
                    case 2: List_LineSeries[i].Name = "功率"; break;
                    default: List_LineSeries[i].Name = $"曲线{i + 1}"; break;
                }
                LineName.Add(List_LineSeries[i].Name.ToString());
                List_LineSeries[i].Stroke = new SolidColorPaint()
                {
                    Color = SKColor.Parse(List_SolidColorPaint[i]),
                    StrokeThickness = 1,
                };
               

                List_LineSeries[i].Fill = null;
                if (i >= 2)
                {
                    List_LineSeries[i].ScalesYAt = 1;
                }
                List_LineSeries[i].LineSmoothness = 1;
                List_LineSeries[i].GeometryFill = null;
                List_LineSeries[i].GeometrySize = 0;
                List_LineSeries[i].GeometryStroke = null;
                List_LineSeries[i].DataLabelsPaint = null;
                List_LineSeries[i].DataLabelsSize = 0;
            }
            List_ISeries_XAxis_Labels = new List<string>() { "" };//预先初始化一个标签，是为了之前已经预先添加了一个数据点，一一对应

            List_ISeries_XAxis_Name = new List<string> { "时间" };

            List_ISeries_YAxis_Name = new List<string> { "数值" };

            ISeries_XAxis_Datatime = new Axis[]
            {
                 new Axis
                {
                     //横坐标名称
                     //Name = List_ISeries_XAxis_Name[0],
                     //NamePaint=new SolidColorPaint()
                     //{
                     //    Color = SKColors.Black,
                     //    SKTypeface = SKFontManager.Default.MatchCharacter('汉'),
                     //},
                     Labels = List_ISeries_XAxis_Labels,
                     LabelsRotation = 0,
                     MaxLimit = ISeries_XAxis_MaxLimit,
                     MinLimit = ISeries_XAxis_MinLimit,
                     ForceStepToMin=true,
                     MinStep=5,
                     SeparatorsPaint=new SolidColorPaint(SKColors.Gray),
                     LabelsPaint =new SolidColorPaint()
                    {
                        Color=SKColors.White
                    }
                 }
            };
            ISeries_XAxis_Normal = new Axis[]
            {
                 new Axis
                {
                     NamePaint=new SolidColorPaint()
                     {
                         Color = SKColors.Black,
                         SKTypeface = SKFontManager.Default.MatchCharacter('汉'),
                     },
                     MaxLimit = ISeries_XAxis_MaxLimit,
                     MinLimit = ISeries_XAxis_MinLimit,
                 }
            };

            ISeries_YAxis_Value = new Axis[]
            {
                 new Axis
                {
                    Name = List_ISeries_YAxis_Name[0],
                    NamePaint=new SolidColorPaint()
                     {
                         Color = SKColors.Black,
                         SKTypeface = SKFontManager.Default.MatchCharacter('汉'),
                     },
                    LabelsPaint =new SolidColorPaint()
                    {
                        Color=SKColors.White
                    }
                }
            };

            ISeries_YAxis_Normal = new Axis[]
            {
                 new Axis
                {
                    NamePaint=new SolidColorPaint()
                     {
                         Color = SKColors.Black,
                         SKTypeface = SKFontManager.Default.MatchCharacter('汉'),
                     },
                    MaxLimit = ISeries_YAxis_MaxLimit,
                    MinLimit = ISeries_YAxis_MinLimit,
                    MinStep=10,
                    MinZoomDelta=40,
                    ForceStepToMin=true,
                    SeparatorsPaint=new SolidColorPaint(SKColors.Gray),
                    LabelsPaint =new SolidColorPaint()
                    {
                        Color=SKColors.White
                    }
                 },
                   new Axis
                {
                    NamePaint=new SolidColorPaint()
                     {
                         Color = SKColors.Black,
                         SKTypeface = SKFontManager.Default.MatchCharacter('汉'),
                     },
                    Position = LiveChartsCore.Measure.AxisPosition.End,
                    MaxLimit = 500,
                    MinLimit = ISeries_YAxis_MinLimit,
                    MinStep=50,
                    MinZoomDelta=40,
                    ForceStepToMin=true,
                    SeparatorsPaint=new SolidColorPaint(SKColors.Gray),
                    LabelsPaint =new SolidColorPaint()
                    {
                        Color=SKColors.White
                    }
                 }
            };

            //图表名称
            ISeries_LabelVisual = new LabelVisual
            {
                //Text = "线性视图",
                //Paint = new SolidColorPaint
                //{
                //    Color = SKColors.Black,
                //    SKTypeface = SKFontManager.Default.MatchCharacter('汉')
                //}
            };

            ISeries_LegendTextPaint = new SolidColorPaint()
            {
                SKTypeface = SKFontManager.Default.MatchCharacter('汉'),
                Color = SKColors.White,
            };

            ToolTipTextPaint = new SolidColorPaint()
            {
                SKTypeface = SKFontManager.Default.MatchCharacter('汉'),
                Color = SKColors.Black,
            };

            ISeries_XAxis_AutoScrollLimit = ISeries_XAxis_MaxLimit * 1.0;

            LiveChartsLineSeriesCountSet(ISeries_LineSeriesMaxCount);
        }

        /// <summary>
        /// 线性序列数量设置，最多10个系列
        /// </summary>
        /// <param name="LineSeriesCount">线性序列数量，最多10个系列</param>
        public void LiveChartsLineSeriesCountSet(int LineSeriesCount = 1)
        {
            switch (LineSeriesCount)
            {
                case 1:
                    {
                        ISeries_LineSeries = new ISeries[]
                        {
                            List_LineSeries[0]
                        };
                    }
                    break;
                case 2:
                    {
                        ISeries_LineSeries = new ISeries[]
                        {
                            List_LineSeries[0],
                            List_LineSeries[1]
                        };
                    }
                    break;
                case 3:
                    {
                        ISeries_LineSeries = new ISeries[]
                        {
                            List_LineSeries[0],
                            List_LineSeries[1],
                            List_LineSeries[2]
                        };
                    }
                    break;
                case 4:
                    {
                        ISeries_LineSeries = new ISeries[]
                        {
                            List_LineSeries[0],
                            List_LineSeries[1],
                            List_LineSeries[2],
                            List_LineSeries[3]
                        };
                    }
                    break;
                case 5:
                    {
                        ISeries_LineSeries = new ISeries[]
                        {
                            List_LineSeries[0],
                            List_LineSeries[1],
                            List_LineSeries[2],
                            List_LineSeries[3],
                            List_LineSeries[4]
                        };
                    }
                    break;
                case 6:
                    {
                        ISeries_LineSeries = new ISeries[]
                        {
                            List_LineSeries[0],
                            List_LineSeries[1],
                            List_LineSeries[2],
                            List_LineSeries[3],
                            List_LineSeries[4],
                            List_LineSeries[5]
                        };
                    }
                    break;
                case 7:
                    {
                        ISeries_LineSeries = new ISeries[]
                        {
                            List_LineSeries[0],
                            List_LineSeries[1],
                            List_LineSeries[2],
                            List_LineSeries[3],
                            List_LineSeries[4],
                            List_LineSeries[5],
                            List_LineSeries[6]
                        };
                    }
                    break;
                case 8:
                    {
                        ISeries_LineSeries = new ISeries[]
                        {
                            List_LineSeries[0],
                            List_LineSeries[1],
                            List_LineSeries[2],
                            List_LineSeries[3],
                            List_LineSeries[4],
                            List_LineSeries[5],
                            List_LineSeries[6],
                            List_LineSeries[7],
                        };
                    }
                    break;
                case 9:
                    {
                        ISeries_LineSeries = new ISeries[]
                        {
                            List_LineSeries[0],
                            List_LineSeries[1],
                            List_LineSeries[2],
                            List_LineSeries[3],
                            List_LineSeries[4],
                            List_LineSeries[5],
                            List_LineSeries[6],
                            List_LineSeries[7],
                            List_LineSeries[8],
                        };
                    }
                    break;
                case 10:
                    {
                        ISeries_LineSeries = new ISeries[]
                        {
                            List_LineSeries[0],
                            List_LineSeries[1],
                            List_LineSeries[2],
                            List_LineSeries[3],
                            List_LineSeries[4],
                            List_LineSeries[5],
                            List_LineSeries[6],
                            List_LineSeries[7],
                            List_LineSeries[8],
                            List_LineSeries[9]
                        };
                    }
                    break;
                default: break;
            }
        }

        /// <summary>
        /// 线性序列名称设置，最多10个系列
        /// </summary>
        /// <param name="LineSeriesNames"></param>
        public void LiveChartsLineSeriesNameSet(List<string> LineSeriesNames)
        {
            for (int i = 0; i < LineSeriesNames.Count; i++)
            {
                List_LineSeries[i].Name = LineSeriesNames[i];
            }
        }

        /// <summary>
        /// 线性系列，开始绘图
        /// </summary>
        /// <param name="PlotMode">绘图模式，false，保留之前的数据，连续绘制；true，清除之前的数据，程序绘制</param>
        /// <param name="LineSeriesNames">线性系列名称</param>
        /// <param name="Values">线性系列数值，数值类型double</param>
        /// <param name="DelayTime">循环延时时间，单位ms</param>
        /// <param name="TimeSpen">时间跨度，单位s</param>
        public void LiveChartsStartPlot(bool PlotMode, List<double> Values, int DelayTime, int TimeSpen)
        {
            List<int> List_ObservableCollectionCount = new List<int>();
            for (int i = 0; i < List_ObservableCollection.Count; i++)
            {
                List_ObservableCollectionCount.Add(List_ObservableCollection[i].Count);
            }
            if (PlotMode)
            {
                List_ObservableCollectionAllClear();
                XAxisLabelsAllClear();
            }
            if (List_ObservableCollectionCount.Max() > 40)
            {
                ISeries_XAxis_Datatime[0].MaxLimit++;
                ISeries_XAxis_Datatime[0].MinLimit++;
            }
            if (List_ObservableCollectionCount.Max() > 3000)
            {
                List_ObservableCollectionAllRemoveAt0();
            }
            if ((DateTime.Now.Second % TimeSpen) == 0)
            {
                if (!isDateTimeLabelAdded)
                {
                    List_ISeries_XAxis_Labels.Add(DateTime.Now.ToLongTimeString());
                    isDateTimeLabelAdded = true;
                }
                else
                {
                    List_ISeries_XAxis_Labels.Add("");
                }
            }
            else
            {
                List_ISeries_XAxis_Labels.Add("");
                isDateTimeLabelAdded = false;
            }
            for (int i = 0; i < List_ObservableCollection.Count; i++)
            {
                List_ObservableCollection[i].Add(new ObservablePoint(List_ObservableCollection[i].Count + List_ObservableCollectionRemovedCount, Values[i]));
            }
        }

        /// <summary>
        /// 清空所有线性序列的动态数据点列表
        /// </summary>
        public void List_ObservableCollectionAllClear()
        {
            foreach (var var_temp in List_ObservableCollection)
            {
                var_temp.Clear();
            }
            List_ObservableCollectionRemovedCount = 0;
        }

        /// <summary>
        /// 清空X轴所有的标签信息
        /// </summary>
        public void XAxisLabelsAllClear()
        {
            List_ISeries_XAxis_Labels.Clear();
            ISeries_XAxis_Datatime[0].MaxLimit = ISeries_XAxis_MaxLimit;
            ISeries_XAxis_Datatime[0].MinLimit = ISeries_XAxis_MinLimit;
        }

        /// <summary>
        /// 移除所有线性序列的第一个动态数据点列表
        /// </summary>
        private void List_ObservableCollectionAllRemoveAt0()
        {
            foreach (var var_temp in List_ObservableCollection)
            {
                var_temp.RemoveAt(0);
            }
            List_ObservableCollectionRemovedCount++;
        }

        /// <summary>
        /// 横坐标和纵坐标缩放重置
        /// </summary>
        public void LiveChartsAxisReset()
        {
            List<int> list_temp = new List<int>();
            List<double> list_temp1 = new List<double>();
            try
            {
                foreach (var var_temp in List_ObservableCollection)
                {
                    list_temp.Add(var_temp.Count);
                    for (int i = 0; i < var_temp.Count; i++)
                    {
                        list_temp1.Add((double)var_temp[i].Y);
                    }
                }
                ISeries_XAxis_Datatime[0].MaxLimit = (list_temp.Max() >= ISeries_XAxis_MaxLimit ? list_temp.Max() : ISeries_XAxis_MaxLimit) + List_ObservableCollectionRemovedCount;
                ISeries_XAxis_Datatime[0].MinLimit = ISeries_XAxis_Datatime[0].MaxLimit - ISeries_XAxis_MaxLimit;
                ISeries_YAxis_Normal[0].MaxLimit = ISeries_YAxis_MaxLimit;
                ISeries_YAxis_Normal[0].MinLimit = 0;
                ISeries_YAxis_Normal[1].MaxLimit = ISeries_YAxis_MaxLimitRight;
                ISeries_YAxis_Normal[1].MinLimit = 0;
            }
            catch { }
        }
    }
}
